<?php

class myCaptchaValidator extends sfValidator
{
  /**
   * Initializes CAPTCHA validator
   *
   */
  public function initialize($context, $parameters = null)
  {
    // initialize parent
    parent::initialize($context);
 
    // set defaults
    $this->setParameter('captcha_error', 'Invalid input');
 
    $this->getParameterHolder()->add($parameters);
    
    return true;  
  }
 
  /**
   * Executes CAPTCHA validation
   *
   */
  public function execute(&$value, &$error)
  {
    if((mb_strtoupper($value) != sfContext::getInstance()->getUser()->getAttribute('captcha')) or (sfContext::getInstance()->getUser()->getAttribute('captcha') == null))
    {
      $error = $this->getParameter('captcha_error');
      return false;
    }
    
    sfContext::getInstance()->getUser()->getAttributeHolder()->remove('captcha');
    return true;
  }
}
